class LimitableMap {
  constructor(limit) {
    this.limit = limit || 10;
    this.map = {}
    this.keys = []
  }
  get(key) {
    return this.map[key]
  }
  set(key, value) {
    if (!Object.prototype.hasOwnProperty.call(this.map, key)) {
      if (this.keys.length === this.limit) {
        let firstKey = this.keys.shift() // 从key缓存数组中移除
        delete this.map[firstKey] // 删除缓存对象中的键值
      }
      this.keys.push(key) // 该key进入缓存
    }
    this.map[key] = value
  }
  getCache() {
    return this.map
  }
}

let limitableMap = new LimitableMap()

for (let i = 0; i < 5; i++) {
  limitableMap.set(i, i)
}
console.log(JSON.parse(JSON.stringify(limitableMap.getCache())))

for (let i = 5; i < 12; i++) {
  limitableMap.set(i, i)
}
console.log(JSON.parse(JSON.stringify(limitableMap.getCache())))